package com.downhometech.productcalculator.activities;

import java.util.ArrayList;

import com.downhometech.productcalculator.controllers.ProductController;
import com.downhometech.productcalculator.models.Product;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ProductListActivity extends DefaultActivity
{
	private ListView listView = null;
	private ArrayList<Product> productList = null;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.productlist);

	    Intent intent = getIntent();
	    ProductController controller = new ProductController(this);
	    productList = new ArrayList<Product>();
	    ArrayAdapter<Product> adapter = null;
	    
	    listView = (ListView)findViewById(R.id.productlist_list);
	    adapter = new ArrayAdapter<Product>(this, R.layout.productlist_item, productList);
    	listView.setAdapter(adapter);
    	
    	Bundle passedInBundle = this.getIntent().getExtras();
    	int passedInId = passedInBundle.getInt("_id");
    	
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
	    {
	    	String query = intent.getStringExtra(SearchManager.QUERY);
	    	productList  = controller.searchForProducts(query);
	    	adapter = new ArrayAdapter<Product>(this, R.layout.productlist_item, productList);
	    	registerForContextMenu(listView);
	    	if (productList.size() == 0)
	    	{
	    		productList.add(new Product(0, "No Products Found","", 0, false, 0));
	    		listView.setAdapter(adapter);
	    	}
	    	else
	    	{
	    		listView.setAdapter(adapter);
				listView.setOnItemClickListener(new OnItemClickListener() 
				{
					public void onItemClick(AdapterView<?> adapter, View view, int adapterLocation, long arg3) 
					{
						Product product = (Product)adapter.getItemAtPosition(adapterLocation);
						Intent intent = new Intent(ProductListActivity.this, ProductDescriptionActivity.class);
						intent.putExtra("_productId", new Integer(product.getId()));
						startActivity(intent);				
					}
				});		    		
	    	}

				    	
	    }
	    else
	    {
			if (passedInId < 0 ) { productList = controller.getAllProducts();  }
			else { productList = controller.getAllProductsForCategory(passedInId); }
	    	adapter = new ArrayAdapter<Product>(this, R.layout.productlist_item, productList);
    		listView.setAdapter(adapter);
    		registerForContextMenu(listView);
			listView.setOnItemClickListener(new OnItemClickListener() 
			{
				public void onItemClick(AdapterView<?> adapter, View view, int adapterLocation, long arg3) 
				{
					Product product = (Product)adapter.getItemAtPosition(adapterLocation);
					Intent intent = null;
					if (product.isCategory()) 
					{
						intent = new Intent(ProductListActivity.this, ProductListActivity.class);
						intent.putExtra("_id", product.getId());  	
					}
					else
					{
						intent = new Intent(ProductListActivity.this, ProductDescriptionActivity.class);
						intent.putExtra("_productId", product.getId());
					}
					startActivity(intent);				
				}
			});		    	
	    }

	}
	
	public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo) 
	{
		if (view.getId()==R.id.productlist_list) 
		{
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			menu.setHeaderTitle(productList.get(info.position).getName());
			String[] menuItems = new String[] { "Edit" };
			for (int i = 0; i<menuItems.length; i++) 
			{
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		int productId = productList.get(info.position).getId();
		if (menuItemIndex == 0)
		{
			Intent intent = new Intent(this, EditProductActivity.class);
			intent.putExtra("_productId", new Integer(productId));
			startActivity(intent);	 
			finish();
		}
		return true;
	}	
	

}
